<template>
  <div class="order">
    <!-- 标题 -->
    <div class="otitle">
      <img @click="fantui" src="./images/right.png" alt>
      <p>确认订单</p>
    </div>
    <!-- 个人信息 -->
    <div class="operson" v-if="isAddress" @click="goToChoseAddress()">
      <div class="person">
        <h6>
          收货人：
          <span>{{address.accept_name}}</span>
        </h6>
        <i>{{address.telphone}}</i>
      </div>
      <p>
        收货地址：
        <span>{{address.province_val}} {{address.city_val}} {{address.area_val}}  {{address.address}}</span>
      </p>
      <img src="./images/turn-r.png" alt class="turn-r">
      <div class="xian">
        <img src="./images/ht.png" alt>
      </div>
    </div>
    <div class="operson2" v-else @click="goToChoseAddress()">
      <h6>提示：</h6>
      <div>点击添加地址</div>
      <van-icon name="arrow" />
    </div>
    <!-- 商品信息 -->
    <div class="thing">
      <div class="things">
        <div class="t-show">
          <div class="ts-left">
            <img :src="goodsList[0].img" alt>
          </div>
          <div class="ts-right">
            <h5>{{goodsList[0].name}}</h5>
            <div class="choose">
              <select name="" id="">
                <option value="1">
                    女装(现货)；L
                </option>
              </select>
            </div>
            <p v-if="Rbol">
              ￥{{goodsList[0].sell_price}}
              <span class="omoney">￥{{goodsList[0].point}}</span>
            </p>
            <p v-else>
              ￥{{this.datas.pric}}
              <span class="omoney">￥{{goodsList[0].point}}</span>
            </p>
          </div>
        </div>
        <div class="t-num">
          <p class="t-num-word">数量</p>
          <div class="number">
            <van-stepper @plus="jia()" @minus="jian()" v-model="num1" />
          </div>
        </div>
        <div class="t-ano">
          <p class="talk">留言：</p>
          <textarea
            class="tcp_content"
            placeholder="特殊需求请留言（45字以内）"
            maxlength="45"
            onchange="this.value=this.value.substring(0,45)"
            onkeydown="this.value=this.value.substring(0,45)"
            onkeyup="this.value=this.value.substring(0,45)"
            v-model="message"
          ></textarea>
        </div>
      </div>
    </div>
    <!-- 优惠券 -->
    <div class="oyhq">
      <table class="youhui">
        <tr class="yh1">
          <td class="yhq1">优惠券</td>
          <td class="yhq21">
            <span class="three1"><span class="three">3</span>张</span>
            <img src="./images/turn-r.png" alt>
          </td>
        </tr>
        <tr class="yh2">
          <td class="yhq1">
            余额：
            <span>0.0元可用</span>
          </td>
          <td class="yhq2">
            <van-switch size="20px" v-model="checked" />
          </td>
        </tr>
        <tr class="yh33">
          <td class="yh3-1">可用{{goodsList[0].exp}}积分抵扣{{goodsList[0].reduce}}元</td>
          <td class="yh3-2">
             <van-switch size="20px" v-model="checked1"/>
          </td>
        </tr>
      </table>
    </div>
    <!-- 运费 -->
    <div class="yunfei">
      <table class="yf">
        <tr>
          <td>
            <p>运费：￥</p>
            <span>{{0.00}}</span>
          </td>
          <td class="zje">
            <p class="zjeword">商品总金额：</p>
            <span class="zjemoney">￥ 884.00</span>
          </td>
        </tr>
      </table>
    </div>
    <!-- 支付 -->
    <div class="opay">
      <div class="paynum">
        <p>应付：￥</p>
        <b>{{number}}</b>
      </div>
      <div @click="Topay()" class="gopay">
        <p>去支付</p>
      </div>
    </div>

    <div class="alert-shadow" v-if="hintAddAdderss">
      <div class="alertPopup">
        <div class="alert-top">
          <div></div>
          <div>提示</div>
          <div @click="closeHintAddAdderss">
            <van-icon name="cross" />
          </div>
        </div>
        <div class="alert-mid">您还没有添加收货地址，请点击添加</div>
        <div class="alert-bottom">
          <div @click="closeHintAddAdderss">取消</div>
          <div @click="goToChoseAddress">添加</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import request from '../../../static/request.js'
import { Notify } from 'vant';
import { Popup } from 'vant';

export default {
  data() {
    return {
      num1:1,
      checked:false,
      checked1:false,
      goodsList:[],
      address:[],      //地址列表
      datas:"",
      message:'',     //留言
      addres:'',    //地址
      number:0,     //应付
      page:0,       //回跳地址
      Rbol:true,    // 是否拼团  默认为否  (treu:否，false:是)

      // 提示添加地址
      hintAddAdderss: false,
      isAddress: true
    };
  },
  components: {
  },
  methods:{
    goToChoseAddress(){
      this.$router.push({
        name: 'choseAddress'
      })
    },
    closeHintAddAdderss(){
      this.hintAddAdderss = false
    },
    fantui(){
      this.$router.go(-1)
    },
    // 商品数量增加
    jia(){
      if(this.Rbol){
        this.num1++;
        this.number = this.num1 * this.goodsList[0].sell_price;
      }else{
        this.num1++;
        this.number = this.num1 * this.datas.pric;
      }
    },
    jian(){
       if(this.Rbol){
        this.num1--;
        this.number = this.num1 * this.goodsList[0].sell_price;
      }else{
        this.num1--;
        this.number = this.num1 * this.datas.pric;
      }
    },
    Topay(){
      if(!this.isAddress){
        this.hintAddAdderss = true
        return
      }
      request({
        url: '?controller=theapi&action=cart3',
        method: 'get',
        params: {
          delivery_id: 1,
          message: this.message,
          payment:1,
          direct_gid:this.datas.gid,
          direct_num: this.datas.num,
          direct_type: this.datas.type,
          radio_address:this.address.id
        }
      }).then(res=>{
        console.log(res);
        if(res.data.return_code == "success"){
          this.$router.push({
            name:'choosePay',
            params:{
              gid: res.data.data.order_id,
              num: this.number,
              tuanD: this.Rbol
            }
          })
        }
      })
    },
     // 获取URL传过来的参数
    // getUrlParam(sUrl, sKey) {
    //   var result = {}
    //   sUrl.replace(/\??(\w+)=(\w+)&?/g,function(all, key, value){
    //       if(result[key] !== void 0){
    //           var t = result[key]
    //           result[key] = [].concat(t,value)
    //       }else{
    //           result[key] = value
    //       }
    //   })
    //   if(sKey === void 0){
    //       return result;
    //   }else{
    //       return result[sKey] || ''
    //   }
    // }
    // 获取订单信息
    getOrderInfo(){
      console.log(this.datas.pric);
      if(this.datas.pinD == 1){
          this.Rbol = false;
          request({
          url: '?controller=theapi&action=cart2',
          method: 'get',
          params: {
            gid:this.datas.gid,
            type: this.datas.type,
            num: this.datas.num
          }
        }).then(res=>{
          console.log(res.data.data)
          if(res.data.data == '请登录'){
            this.$router.push({
              name: 'login'
            })
          }
          if(res.data.data.addressList == 0){
            this.hintAddAdderss = true
            this.isAddress = false
          }else{
            this.isAddress = true
          }
          this.goodsList = res.data.data.goods.goodsList;
          // 从选择地址重回来判断缓存中是否有地址
          let addressInfo = JSON.parse(localStorage.getItem('localStorageAddress'))
          console.log(addressInfo)
          if(!addressInfo){
            this.address = res.data.data.addressList[0];
          }else{
            let addressObj = {
              accept_name: addressInfo.name,
              address: addressInfo.addressM,
              area_val: addressInfo.area_val,
              city_val: addressInfo.city_val,
              province_val: addressInfo.province_val,
              telphone: addressInfo.tel,
              zip: addressInfo.zip,
              id: addressInfo.id
            }
            this.address = addressObj
            // 赋值后删除地址缓存
            localStorage.removeItem('localStorageAddress')
          }
          this.addres = res.data.data.DeliveryList
          this.num1 = res.data.data.goods.count;
          // 计算总价
          this.number = this.goodsList[0].count * this.datas.pric;
        })
      }else if(this.datas.pinD == 0){
        request({
          url: '?controller=theapi&action=cart2',
          method: 'get',
          params: {
            gid:this.datas.gid,
            type: this.datas.type,
            num: this.datas.num
          }
        }).then(res=>{
          console.log(res.data.data)
          if(res.data.data == '请登录'){
            this.$router.push({
              name: 'login'
            })
          }
          if(res.data.data.addressList == 0){
            this.hintAddAdderss = true
            this.isAddress = false
          }else{
            this.isAddress = true
          }
          this.goodsList = res.data.data.goods.goodsList;
          // 从选择地址重回来判断缓存中是否有地址
          let addressInfo = JSON.parse(localStorage.getItem('localStorageAddress'))
          console.log(addressInfo)
          if(!addressInfo){
            this.address = res.data.data.addressList[0];
          }else{
            let addressObj = {
              accept_name: addressInfo.name,
              address: addressInfo.addressM,
              area_val: addressInfo.area_val,
              city_val: addressInfo.city_val,
              province_val: addressInfo.province_val,
              telphone: addressInfo.tel,
              zip: addressInfo.zip,
              id: addressInfo.id
            }
            this.address = addressObj
            // 赋值后删除地址缓存
            localStorage.removeItem('localStorageAddress')
          }
          this.addres = res.data.data.DeliveryList
          this.num1 = res.data.data.goods.count;
          // 计算总价
          this.number = this.goodsList[0].count * this.goodsList[0].sell_price;
        })
       }
    }
  },
  created(){
    this.datas = this.$route.params;
    // let data = this.getUrlParam(window.location.href)
    // this.datas = data;
    console.log(this.datas);

    if(!this.datas.gid){
      this.datas = JSON.parse(localStorage.getItem('thisDatas'))
      console.log(this.datas)
    }
    localStorage.setItem('thisDatas',JSON.stringify(this.datas))
    // this.page = this.datas.page;

    // 请求定义在方法中了
    this.getOrderInfo()
  }
};
</script>

<style scoped lang="scss">
  .save-address{
    padding: 0.3rem 0 0 0;
    margin: 0.3rem;
    display: flex;
    align-items: center;
    justify-content: center;
    .save{
      flex-grow: 1;
    }
  }
  .go-black{
    display: flex;
    justify-content: flex-start;
    align-items: center;
    font-size: 0.3rem;
    padding: 0.2rem;
  }
  .alert-shadow{
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 99;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .alertPopup{
    width:6.20rem;
    border-radius: 0.2rem;
    background: #ffffff;
  }
  .alert-top{
    display: flex;
    padding: 0 0.2rem;
    height: 0.8rem;
    justify-content: space-between;
    align-items: center;
    border-bottom: 0.01rem solid #EFF1F3;
    div{
      width: 0.6rem;
      font-size: 0.3rem;
    }
    div:last-of-type{
      text-align: right;
    }
  }
  .alert-mid{
    height: 2.77rem;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.3rem;
  }
  .alert-bottom{
    display: flex;
    padding: 0 0.2rem;
    height: 0.8rem;
    justify-content: space-between;
    align-items: center;
    border-top: 0.01rem solid #EFF1F3;
    div{
      width: 50%;
      text-align: center;
      font-size: 0.3rem;
      line-height: 0.8rem;
    }
    div:first-of-type{
      border-right: 0.01rem solid #EFF1F3;
    }
  }
</style>
<style>
.order {
  width: 100%;
  /* height: 10rem; */
  background: #f5f6f7;
}
.otitle {
  width: 100%;
  height: 0.88rem;
  position: relative;
  background: #fff;
}
.otitle img {
  width: 0.22rem;
  height: 0.32rem;
  position: absolute;
  top: 50%;
  margin-top: -0.14rem;
  left: 0.3rem;
}

.otitle p {
  font-size: 0.32rem;
  font-weight: bold;
  font-family: PingFang-SC-Bold;
  text-align: center;
  line-height: 0.9rem;
  color: #191e2a;
}
.operson {
  width: 100%;
  position: relative;
  margin-top: 0.2rem;
  background: #fff;
}
.operson2{
  display: flex;
  margin-top: 0.2rem;
  position: relative;
  padding: 0.3rem;
  height: 1rem;
  font-size: 0.3rem;
  justify-content: space-between;
  align-items: center;
  background: #fff;
}
.person {
  width: 92%;
  height: 0.3rem;
  padding-top: 0.3rem;
  box-sizing: border-box;
  /* margin-left: 0.3rem; */
  margin: 0 auto;
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
}
.person h6 {
  display: inline-block;
  font-size: 0.28rem;
  font-weight: bold;
  color: #191e2a;
  font-family: PingFang-SC-Bold;
}
.person i {
  display: inline-block;
  font-style: normal;
  color: #191e2a;
  font-size: 0.26rem;
  font-weight: medium;
}
.operson p {
  font-size: 0.28rem;
  color: #191e2a;
  font-weight: medium;
  line-height: 0.44rem;
  clear: both;
  display: inline-block;
  width: 78%;
  margin-top: 0.6rem;
  padding-left: 0.3rem;
  padding-right: 0.92rem;
}
.turn-r {
  width: 0.14rem;
  height: 0.2rem;
  position: absolute;
  top: 1.2rem;
  left: 95%;
}
.xian {
  margin-top: 0.12rem;
}
.xian img {
  width: 100%;
  height: 0.1rem;
}
.thing {
  width: 100%;
  background: #fff;
  margin-top: 0.2rem;
}
.things {
  height: 5.3rem;
}
.t-show {
  width: 92%;
  height: 2.6rem;
  padding-top: 0.3rem;
  box-sizing: border-box;
  margin: 0 auto;
  /* background: yellow; */
}
.ts-left {
  width: 2.3rem;
  height: 2.3rem;
  float: left;
  overflow: hidden;
  border-radius: 0.1rem;
  border: 0.01rem solid #eaeaea;
}
.ts-left img {
  width: 2.2rem;
  height: 2.2rem;
  margin-top: 0.05rem;
  margin-left: 0.05rem;
}
.ts-right {
  width: 4.2rem;
  float: left;
  margin-left: 0.3rem;
}
.ts-right h5 {
  font-size: 0.3rem;
  color: #191e2a;
  font-weight: bold;
  line-height: 0.4rem;
}
.ts-right .choose {
  width: 2.3rem;
  height: 0.35rem;
  margin-top: 0.4rem;
  margin-bottom: 0.4rem;
  border-radius: 0.06rem;
  /* background: #f5f5f6; */
  position: relative;
}
.ts-right .choose select{
  border: 0rem;
  outline: none;
  height: 0.38rem;
  border-radius: 0.1rem;
  background: #f5f5f6;
}
.ts-right .choose p {
  font-size: 0.22rem;
  color: #c1c3c6;
  display: inline-block;
}
.ts-right .choose i {
  width: 0.14rem;
  height: 0.09rem;
  display: inline-block;
  position: absolute;
  top: -0.04rem;
  left: 1.9rem;
}
.ts-right .choose i img {
  width: 0.14rem;
  height: 0.09rem;
}
.ts-right p {
  font-size: 0.26rem;
  color: #f52e67;
  font-size: medium;
  font-weight: bold;
}
.ts-right p .omoney {
  display: inline-block;
  font-weight: 200;
  color: #b3b5b8;
  margin-left: 0.4rem;
  text-decoration: line-through;
}
.t-num {
  margin: 0 auto;
  width: 92%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.35rem 0;
  border-bottom: 0.01rem solid #efefef;
}
.t-num-word {
  font-size: 0.3rem;
  color: #191e2a;
}
.jian,
.jia {
  width: 0.5rem;
}
.sum {
  width: 0.76rem;
  height: 0.55rem;
  border-left: 0.01rem solid #dedfe3;
  border-right: 0.01rem solid #dedfe3;
}
.t-ano {
  width: 92%;
  height: 1.4rem;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
  /* background: yellowgreen; */
}
.talk {
  font-size: 0.3rem;
  color: #191e2a;
  line-height: 0.8rem;
  float: left;
}
.tcp_content {
  float: left;
  width: 86%;
  height: 0.6rem;
  margin-top: 0.26rem;
  color: #9fa0a5;
  font-size: 0.28rem;
  border-style: none;
  outline: none;
  resize: none;
  overflow: hidden;
}

.oyhq {
  width: 100%;
  background: #fff;
  margin-top: 0.2rem;
}
.youhui {
  width: 93%;
  height: 3.02rem;
  /* background: red; */
  margin: 0 auto;
}
.youhui tr {
  width: 100%;
  height: 1rem;
  /* background: #191e2a; */
}
.youhui tr td {
  font-size: medium;
  font-size: 0.3rem;
  line-height: 1rem;
  color: #191e2a;
}

.yh1 .yhq1,
.yh2 .yhq1,
.yh33 .yh3-1 {
  width: 76%;
}
.yh1 {
  width: 100%;
}
.yh1 .yhq2 {
  display: block;
  /* margin-left: 2.5rem; */
  /* line-height: 1rem; */
}
.yhq21{
  text-align: right;
}
.yh1 .yhq2 .three {
  color: #f52e67;
}
.yh1 .yhq2 img {
  width: 0.14rem;
  height: 0.2rem;
  float: left;
  margin: 0 0.03rem;
  margin-top: 0.4rem;

}
.yh2 {
  border-top: 0.01rem solid #eff1f3;
  border-bottom: 0.01rem solid #eff1f3;
}
.yh2 .yhq1 span {
  color: #9fa0a5;
}
.yh2 .yhq2 {
  display: block;
  margin-left: 2.4rem;
  padding-top: 0.4rem;
  box-sizing: border-box;
}
.yh2 .yhq2 img {
  width: 1rem;
  height: 0.6rem;
}
.yh33 .yh3-1 {
  color: #9fa0a5;
}
.yh33 .yh3-2 {
  display: block;
  margin-left: 2.4rem;
  padding-top: 0.4rem;
  box-sizing: border-box;
}
.yh33 .yh3-2 img {
  width: 1rem;
  height: 0.6rem;
}
/* 运费 */
.yunfei {
  width: 100%;
  background: #fff;
  margin-top: 0.2rem;
  margin-bottom: 1.2rem;
}
.yf {
  width: 75%;
  height: 1.3rem;
  margin-left: 20%;
}
.yf tr td {
  overflow: hidden;
  line-height: 1.3rem;
}
.yf tr td p {
  font-size: 0.3rem;
  color: #9fa0a5;
  float: left;
}
.yf tr td span {
  font-size: 0.3rem;
  color: #9fa0a5;
  float: left;
}
.zje {
  display: block;
  margin-left: 0.6rem;
}
.zje .zjemoney {
  color: #f52e67;
}
/* 支付 */
.opay {
  position: fixed;
  width: 100%;
  height: 1rem;
  bottom: 0;
  z-index: 20;
  /* margin-top: 1.1rem; */
}
.paynum {
  width: 62%;
  height: 1rem;
  float: left;
  overflow: hidden;
  line-height: 1rem;
  background: #22262f;
}
.paynum p {
  font-size: 0.3rem;
  color: #fff;
  font-weight: bold;
  float: left;
  margin-left: 31%;
}
.paynum b {
  font-size: 0.3rem;
  color: #fff;
  float: left;
}
.gopay {
  width: 38%;
  height: 1rem;
  float: left;
  background: #f52e67;
}
.gopay p {
  font-size: 0.3rem;
  color: #fff;
  font-weight: bold;
  text-align: center;
  line-height: 1rem;
}
</style>
